package com.aaa.ssm.controller;

import com.aaa.ssm.entity.EmpDTO;
import com.aaa.ssm.entity.PermissionDTO;
import com.aaa.ssm.result.AjaxResult;
import com.aaa.ssm.service.EmpService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
@ApiImplicitParams({
        @ApiImplicitParam(name="username",value="账号",
        required = true ,type ="String"),
        @ApiImplicitParam(name="password",value="密码",
                required = true ,type ="String"),
})
@RestController
@Api(tags = "员工信息")
@RequestMapping("emp")
public class EmpController {
    @Autowired
    private EmpService empService;
    @Autowired
    HttpSession session;

    @ApiOperation(value = "员工的登录")
    @PostMapping("login")
    public AjaxResult login(String username,String password){
        try {
            EmpDTO login = empService.login(username, password);
            session.setAttribute("emp",login);

            return AjaxResult.success();
        }catch(Exception e){
            e.printStackTrace();
        return AjaxResult.error(e.getMessage());
        }

    }

    @ApiOperation(value = "获取员工权限的信息")
    @GetMapping("getInfo")
    public AjaxResult getInfo(){

            EmpDTO emp = (EmpDTO) session.getAttribute("emp");
            //根据员工信息查询当前员工的权限信息
         List<PermissionDTO> permissions = empService.getPermissionsByRid(emp.getRid());
         HashMap<Object,Object> map=new HashMap<>();
            System.out.println(permissions);
        map.put("userInfo",emp);
        map.put("permissionInfo",permissions);
        return AjaxResult.success(map);



    }
    @GetMapping("send")
    public AjaxResult sendCode(String phone){
       String ok =empService.sendCode(phone);
       return  AjaxResult.success(ok);
    }
}
